package com.ww.microrpc.netty.handler.server;

import com.ww.microrpc.netty.protocol.ResponseMessage;
import com.ww.microrpc.netty.protocol.PingMessage;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * RPC Handler（RPC request processor）
 *
 * @author ww
 */
public class PingMessageHandler extends SimpleChannelInboundHandler<PingMessage> {

    private static final Logger logger = LoggerFactory.getLogger(PingMessageHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, PingMessage message) throws Exception {

        ResponseMessage response = new ResponseMessage();
        response.setRequestId(message.getRequestId());
        response.setResult(message);
        channelHandlerContext.writeAndFlush(response).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                logger.info("Send response for request id: {}", message.getRequestId());
            }
        });
    }
}
